package model

import "github.com/jinzhu/gorm"

type Tag struct {
	Model

	Name  string `json:"name" gorm:"size:128;unique"` // 标签名称
	State uint8  `json:"state" gorm:"index"`          // 状态 0：禁用，1启用
	Total uint   `json:"total"`                       // 关联文章数
}

func (t Tag) TableName() string {
	return "tag"
}

func (t *Tag) FirstOrCreate(tx *gorm.DB) error {
	t.State = 1
	return tx.FirstOrCreate(t, "name = ?", t.Name).Error
}

func (t *Tag) Delete(tx *gorm.DB) error {
	return tx.Delete(t, "id = ?", t.Id).Error
}

func (t *Tag) List(tx *gorm.DB) (tags []Tag, err error) {
	if err := tx.Find(&tags).Error; err != nil {
		return nil, err
	}

	return tags, nil
}
